<%@page import="com.util.ImageHelper"%>
<%@page import="com.liferay.portal.service.ImageLocalServiceUtil"%>
<%@page import="com.liferay.util.portlet.PortletProps"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@ include file="/html/init.jsp" %>

<%@page import="com.liferay.portal.webserver.WebServerServletTokenUtil"%>
<%@page import="com.slayer.model.ShoppingCategoryExt"%>
<%@page import="com.slayer.service.ShoppingCategoryExtLocalServiceUtil"%>
<%@page import="com.slayer.service.ShoppingCartExtLocalServiceUtil"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.HashMap"%>

<%@page import="java.util.Map"%>
<%@page import="javax.portlet.PortletSession"%>
<%@page import="com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil"%>

<%
	List<ShoppingCategory> categories = (List<ShoppingCategory>)request.getAttribute("CATEGORIES");

	List<ShoppingItem> items = (List<ShoppingItem>)request.getAttribute("ITEMS");
	
	PortletURL fetchURL = renderResponse.createRenderURL();
	//fetchURL.setParameter("backURL", themeDisplay.getURLCurrent());
	
	long parentCategoryId = ParamUtil.getLong(request, "parentCategoryId");
	
	List assortedList = new ArrayList();
	
	if (categories != null && !categories.isEmpty()) {
		assortedList.addAll(categories);
	}
	
	if (items != null && !items.isEmpty()) {
		assortedList.addAll(items);
	}
%>

<c:if test="<%= parentCategoryId > 0 %>">
	<%
		String backURL = ParamUtil.getString(request, "backURL");
	
		ShoppingCategory parentCategory = ShoppingCategoryLocalServiceUtil.fetchShoppingCategory(parentCategoryId);
	%>
	  <liferay-ui:header
		backLabel="&laquo; One Level Up" 
		title="<%= parentCategory.getName() %>" 
		backURL="<%= backURL %>"
	 />  
</c:if>

<table class="donate_now">
	<tr>
	<%
		int count = 0;
		for (Object obj: assortedList) {
			%>
				<td>
					<c:choose>
						<c:when test="<%= obj instanceof ShoppingCategory %>">
							<% 
								ShoppingCategory shoppingCategory = (ShoppingCategory)obj; 
							
								long categoryId = shoppingCategory.getCategoryId();
								ShoppingCategoryExt shoppingExt = ShoppingCategoryExtLocalServiceUtil.fetchShoppingCategoryExt(categoryId);
								
								String defaultimageURL = ImageHelper.getImageURL(themeDisplay, shoppingExt);
								
								/*
								String defaultimageURL = PortletProps.get("default.category.image.url");
								
								if (Validator.isNotNull(shoppingExt)) {
									if (Validator.isNotNull(shoppingExt.getImageURL())) {
										defaultimageURL = shoppingExt.getImageURL();
									}
									else if(Validator.isNotNull(ImageLocalServiceUtil.getImage(shoppingExt.getImageId()))){
										
										defaultimageURL= themeDisplay.getPathImage() + "/shopping/item?img_id=" + shoppingExt.getImageId() + "&t=" + WebServerServletTokenUtil.getToken(shoppingExt.getImageId());
									}
									else{
										//do nothing
									}
								}
								*/
										
								
								fetchURL.setParameter("parentCategoryId", String.valueOf(categoryId));
							%>
							<div class="solid-box">
								<p><img alt="<%= shoppingCategory.getName() %>" src="<%= defaultimageURL %>" vspace="0"/></p>
								<h3><%= shoppingCategory.getDescription() %></h3>
							   <%
								fetchURL.setParameter("category", shoppingCategory.getName());
								fetchURL.setParameter("action", "categoryView");				
							   %>
								
								<p><a href="<%= fetchURL.toString() %>"><%= shoppingCategory.getName() %></a></p>
							</div>
						</c:when>
						<c:otherwise>
							<% 
								ShoppingItem shoppingItem = (ShoppingItem) obj;
								long itemId = shoppingItem.getItemId();
								fetchURL.setParameter("itemId", String.valueOf(itemId));
							%>
							  <%
								fetchURL.setParameter("item", shoppingItem.getName());
								fetchURL.setParameter("action", "itemView");				
							   %>
							
							<a href="<%= fetchURL.toString() %>"><%= shoppingItem.getName() %></a>
							<%=shoppingItem.getDescription() %>
						</c:otherwise>
					</c:choose>
				</td>
				<c:if test="<%= ((count+1) % 3 == 0) %>">
					</tr><tr>
				</c:if>
			<%
			count++;
		}
		
		int itemsInLastRow = (count % 3);
		
		if (itemsInLastRow >=1 && itemsInLastRow <= 2) {
			int emptyBoxes = 3-itemsInLastRow;
			for (int i=0; i<emptyBoxes; i++) {
				%><td>&nbsp;</td><% 
			}
			%></tr><%
		}
	%>
	</tr>
</table>
